﻿using System;
using System.Text;
using System.Text.RegularExpressions;

namespace Hongapo_Common
{
    public sealed class TypeConvert
    {
        private TypeConvert() { }

        public static string ToHexString(byte[] byteArray)
        {
            StringBuilder strHex = new StringBuilder();

            foreach (byte b in byteArray)
            {
                strHex.Append(b.ToString("X2", System.Globalization.CultureInfo.CurrentCulture));
            }

            return strHex.ToString();
        }

        public static byte[] FromHexString(string hexString)
        {
            char[] charArray = hexString.ToCharArray();
            int len = charArray.Length / 2;
            byte[] byteArray = new byte[len];

            for (int i = 0; i < len; i++)
            {
                string str = charArray[i * 2].ToString() + charArray[i * 2 + 1].ToString();
                byteArray[i] = Convert.ToByte(str, 16);
            }

            return byteArray;
        }

        public static decimal ToDecimal(string value)
        {
            return ToDecimal(value, 0);
        }

        public static decimal ToDecimal(string value, decimal defaultValue)
        {
            if (string.IsNullOrEmpty(value) || value.Trim().Length >= 11 || !Regex.IsMatch(value.Trim(), @"^([-]|[0-9])[0-9]*(\.\w*)?$"))
                return defaultValue;

            Decimal result;

            if (Decimal.TryParse(value, out result))
                return result;
            else
                return defaultValue;
        }

        public static int ToInt32(string value)
        {
            return ToInt32(value, 0);
        }

        public static int ToInt32(string value, int defaultValue)
        {
            if (string.IsNullOrEmpty(value) || value.Trim().Length >= 11 || !Regex.IsMatch(value.Trim(), @"^([-]|[0-9])[0-9]*(\.\w*)?$"))
                return defaultValue;

            Int32 result;

            if (Int32.TryParse(value, out result))
                return result;

            return Convert.ToInt32(ToFloat(value, defaultValue));
        }

        public static int ToInt32(object value, int defaultValue)
        {
            if (null == value)
                return defaultValue;

            return ToInt32(value.ToString(), defaultValue);
        }

        public static int ToInt32(object value)
        {
            return ToInt32(value, 0);
        }

        public static float ToFloat(string value)
        {
            return ToFloat(value, 0);
        }

        public static float ToFloat(string value, float defaultValue)
        {
            if ((value == null) || (value.Length > 10))
                return defaultValue;

            float result = defaultValue;

            if (value != null)
            {
                bool isFloat = Regex.IsMatch(value, @"^([-]|[0-9])[0-9]*(\.\w*)?$");

                if (isFloat)
                    float.TryParse(value, out result);
            }

            return result;
        }

      

        public static DateTime ToDateTime(string strDate, DateTime defaultValue)
        {
            if (string.IsNullOrEmpty(strDate))
                return defaultValue;

            DateTime result;

            if (DateTime.TryParse(strDate, out result))
                return result;

            return defaultValue;
        }
    }
}
